package com.qinggeng.project.api.controller;

import com.qinggeng.common.enums.ShowTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.project.api.dto.SingleItemRefundDTO;
import com.qinggeng.project.api.security.annotation.LoginRequired;
import com.qinggeng.project.api.security.utils.ApiUtils;
import com.qinggeng.project.system.domain.OrdersGoods;
import com.qinggeng.project.system.domain.UserAccountRecordOrderSingleItem;
import com.qinggeng.project.system.domain.vo.CourierRefundOrdersVO;
import com.qinggeng.project.system.service.IOrdersGoodsService;
import com.qinggeng.project.system.service.UserAccountRecordOrderSingleItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("${apiPath}/orders/single")
public class ApiOrdersSingleItemRefundController extends BaseController {

    @Autowired
    private   UserAccountRecordOrderSingleItemService userAccountRecordOrderSingleItemService;

    @Autowired
    private IOrdersGoodsService ordersGoodsService;

    @PostMapping("/refund")
    @LoginRequired
    public AjaxResult refund(SingleItemRefundDTO dto) {
        dto.setUserId(ApiUtils.getUserId());
        userAccountRecordOrderSingleItemService.orderSingleItemRefund(dto);
        return AjaxResult.success();
    }

    @GetMapping("/headRefundList")
    @LoginRequired(role = ApiUtils.DEFAULT_ROLE_USER)
    public AjaxResult headRefundList(Long courierId,String refundAplStatus){
        if (courierId == null){
            throw new CustomException("courierId is null");
        }
        startPage();
        List<CourierRefundOrdersVO> courierRefundOrdersVOS = userAccountRecordOrderSingleItemService.refundListByCourierId(courierId,refundAplStatus);

        return AjaxResult.success(courierRefundOrdersVOS);
    }

    @GetMapping("/headRefundOrdersGoodsInfo")
    @LoginRequired(role = ApiUtils.DEFAULT_ROLE_USER)
    public AjaxResult headRefundOrdersGoodsInfo(Long ordersGoodsId){
        UserAccountRecordOrderSingleItem userAccountRecordOrderSingleItem = userAccountRecordOrderSingleItemService.getUserAccountRecordOrderSingleItem(ordersGoodsId);
        if (userAccountRecordOrderSingleItem == null){
            throw new CustomException("userAccountRecordOrderSingleItem is null");
        }
        OrdersGoods ordersGoods = ordersGoodsService.selectOrdersGoodsById(userAccountRecordOrderSingleItem.getOrderGoodsId());
        if (ordersGoods == null){
            throw new CustomException("ordersGoods is null");
        }
        userAccountRecordOrderSingleItem.setGoodsName(ordersGoods.getGoodsName());
        userAccountRecordOrderSingleItem.setRefundAplStatus(ordersGoods.getRefundAplStatus());
        userAccountRecordOrderSingleItem.setGoodsImg(ordersGoods.getGoodsImg());
        userAccountRecordOrderSingleItem.setGoodsNum(ordersGoods.getGoodsNum());
        userAccountRecordOrderSingleItem.setGoodsPrice(ordersGoods.getGoodsPrice());
        return AjaxResult.success(userAccountRecordOrderSingleItem);
    }

    @RequestMapping("/updateRefundOrdesGoodsStatus")
    @LoginRequired(role = ApiUtils.DEFAULT_ROLE_USER)
    public AjaxResult updateRefundOrdesGoodsStatus(Long ordersGoodsId,String refundAplStatus){
        boolean b = userAccountRecordOrderSingleItemService.updateRefundOrdesGoodsStatus(ordersGoodsId, refundAplStatus);
        if (b){
            return AjaxResult.success("操作成功",ShowTypeEnum.用户.getValue());
        }
        return AjaxResult.error("操作失败", ShowTypeEnum.用户.getValue());
    }

    @RequestMapping("/deleteHeadRefundOrders")
    @LoginRequired(role = ApiUtils.DEFAULT_ROLE_USER)
    public AjaxResult deleteHeadRefundOrders(Long ordersGoodsId){
        //删除状态
        String refundAplDel = "1";
        boolean b = ordersGoodsService.updateByRefundAplDelByordersGoodsId(ordersGoodsId, refundAplDel);
        if (b){
            return AjaxResult.success("操作成功");
        }

        return AjaxResult.error("操作失败");
    }

}
